# Makefile for interrupt demo

#TOOLS = arm-linux-gnu
#TOOLS = /home/jjinl/gcc_set/gcc-linaro-5.3-2016.02-x86_64_arm-eabi/bin/arm-eabi
TOOLS = /home/jjinl/gcc_set/gcc-arm-none-eabi-8-2019-q3-update/bin/arm-none-eabi

# Assembling with gcc makes it want crt0 at link time.
#AS = $(TOOLS)-gcc
AS = $(TOOLS)-as

#COPTS = -mcpu=cortex-m3 -mthumb -Wno-implicit-function-declaration
COPTS = -mcpu=cortex-m0 -mthumb -Wno-implicit-function-declaration -fno-builtin-printf
# Use the -g flag if you intend to use gdb
#CC = $(TOOLS)-gcc $(COPTS) -g 
#CC = $(TOOLS)-gcc $(COPTS)
CC = $(TOOLS)-gcc $(COPTS) -Os

#LD = $(TOOLS)-gcc
LD = $(TOOLS)-ld.bfd
OBJCOPY = $(TOOLS)-objcopy
DUMP = $(TOOLS)-objdump -d
GDB = $(TOOLS)-gdb
SIZE = $(TOOLS)-size

OUTPUT_DIR = build
OBJS = main.o nvic.o rcc.o gpio.o prf.o serial.o timer.o usb.o usb_desc.o
OUT_OBJS = $(addprefix $(OUTPUT_DIR)/, $(OBJS))


all: usb1.elf usb1.dump usb.bin

usb1.dump:	usb1.elf
	$(DUMP) usb1.elf >usb1.dump
	$(TOOLS)-objdump -DS usb1.elf >usb1.lst

usb.bin: usb1.elf
	$(OBJCOPY) usb1.elf usb.bin -O binary
	$(OBJCOPY) -O ihex usb1.elf usb1.hex

usb1.elf: $(OUTPUT_DIR)/locore.o $(OUT_OBJS) 
	$(LD) -T src/stm_103c8.lds -o usb1.elf $^
	$(SIZE) -t usb1.elf

$(OUTPUT_DIR)/locore.o: src/locore.s
	$(AS) $< -o $@

$(OUTPUT_DIR)/%.o:src/%.c   
	$(CC) $(CFLAGS) -o $@ -c $<  

prog:usb.bin
	./tool/stm32flash -w usb.bin /dev/ttyUSB0

clean:
	rm -f *.o usb1.elf usb.bin usb1.dump build/*.o
